import * as qiniu from 'qiniu-js'

export const Qiniu = {
	Bucket: 'fmcdn',
	Port: 9000,
	UptokenUrl: 'uptoken'
	Domain: 'http: //rg7siaunk .hd-bkt.clouddn.com/',
	uploadUrl: 'https://up-z2.giniup .com',
}

const config = {
	useCdnDomain: true, // 使用cdn加速checkByMD5: true，// 是否开启 MD5 校验
	region: "qiniu.region.zQ",// 华东
	retryCount: 10,// 上传自动重试次数
}
const putExtra =(mimeType) => ({
	fname :"",
	params: {},
	mimeType,})

/**
 * 七牛云上传
 * @param file
 * @param key
 * @param token
 * @param putExtra
 * @param config
 * @returns {Promise<unknown>}
 */
export const uploadToQiNiu = async ({ file, key, token, putExtra, config }) => {
  const observable = qiniu.upload(file, key, token, putExtra, config);
  return new Promise((resolve, reject) => {
    const subscription = observable.subscribe({
      next(result) {
        // setUploadViewInfo({ percent: result.total.percent });
        resolve({ percent:result.total.percent, url:'', status: 'loading' });
      },
      error(err) {
        if (err.message === 'file type doesn\'t match with what you specify') {
          message.warning('上传文件类型不对');
          return;
        }
        message.warning('上传失败');
        reject(err);
      },
      complete(res) {
        const fileName = Qiniu.Domain + res.key;
        // setUploadViewInfo({ percent: 100, url: fileName });
        resolve({ percent: 100, url: fileName, status: 'done' });
      },
    });
  });
};